﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace RainwaterTankEstimator
{
    public class Datum
    {
        public string Name
        {
            get;
            set;
        }

        public float Value
        {
            get;
            set;
        }
    }

    public class AdvancedDatum
    {
        public List<int> Data
        {
            get;
            set;
        }

        public int Value
        {
            get
            {
                return (int)this.Data.Average();
            }
        }
    }

    public class Counter
    {
        public List<bool> Data
        {
            get;
            set;
        }

        public int Value
        {
            get
            {
                return (int)(this.Data.Count(x => x) / this.Data.Count);
            }
        }
    }

    public class MonthDataViewModel
    {
        public static string[] months = { "none", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
        public static int[] monthLen = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

        public ObservableCollection<Datum> Data
        {
            get;
            set;
        }

        public ObservableCollection<Datum> DemandData
        {
            get;
            set;
        }

        public ObservableCollection<Datum> AdjustedData
        {
            get
            {
                var result = new ObservableCollection<Datum>();

                for (int i = 0; i < 4; i++)
                {
                    result.Add(new Datum() { Name = this.Data[i].Name, Value = (int)this.Data[i].Value * this.Days / this.DaysWithWater.Data.Count });
                }

                return result;
            }
        }

        public Counter DaysWithWater
        {
            get;
            set;
        }

        public AdvancedDatum AvgWaterLevel
        {
            get;
            set;
        }

        public string Name
        {
            get;
            private set;
        }

        public int Days
        {
            get;
            private set;
        }

        public MonthDataViewModel(int month)
        {
            this.Name = months[month];
            this.Days = monthLen[month];
            this.Data = new ObservableCollection<Datum>();
            this.Data.Add(new Datum() { Name = "Inefficiency loss", Value = 0 });
            this.Data.Add(new Datum() { Name = "First flush", Value = 0 });
            this.Data.Add(new Datum() { Name = "Overflow", Value = 0 });
            this.Data.Add(new Datum() { Name = "Storage tank", Value = 0 });

            this.DemandData = new ObservableCollection<Datum>();
            this.DemandData.Add(new Datum() { Name = "> 99%", Value = 0 });
            this.DemandData.Add(new Datum() { Name = "50 - 99%", Value = 0 });
            this.DemandData.Add(new Datum() { Name = "1 - 50%", Value = 0 });
            this.DemandData.Add(new Datum() { Name = "< 1%", Value = 0 });

            this.DaysWithWater = new Counter() { Data = new List<bool>() };
            this.AvgWaterLevel = new AdvancedDatum() { Data = new List<int>() };
        }

        public void AddData(float loss, float ff, float o, float tank, int level, float demand)
        {
            this.Data[0].Value += loss;
            this.Data[1].Value += ff;
            this.Data[2].Value += o;
            this.Data[3].Value += tank;

            this.DaysWithWater.Data.Add(level > 0);
            this.AvgWaterLevel.Data.Add(level);

            if (demand > 0.99)
            {
                this.DemandData[0].Value += 1;
            }
            else if(demand <= 0.99 && demand > 0.5)
            {
                this.DemandData[1].Value += 1;
            }
            else if (demand <= 0.5 && demand > 0.01)
            {
                this.DemandData[2].Value += 1;
            }
            else
            {
                this.DemandData[3].Value += 1;
            }
        }
    }
}
